home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / perf_harness / gl_harness.c next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  3.6 KB  |  155 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1996. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <GL/glut.h>
  12.  
  13. extern testInit(int argc, char **argv, int width, int height);
  14. extern testRender(void);
  15.  
  16. int testIterationsStep, testDisplayMode, testMinimumTestTime;
  17. float timeEnd, timeStart;
  18. int error;
  19. int renders = 0, damaged = 0;
  20.  
  21. /* ARGSUSED */
  22. void
  23. report(int value)
  24. {
  25.   float duration;
  26.  
  27.   duration = (timeEnd - timeStart) / 1000.0;
  28.   printf("Renders/second = %g\n",
  29.     renders / duration);
  30.   printf("  after %d iterations over %g seconds\n",
  31.     renders, duration);
  32.   if (error != GL_NO_ERROR)
  33.     printf("OpenGL errors occurred during test; RESULTS ARE DUBIOUS.\n");
  34.   if (damaged != 1)
  35.     printf("Window disturbed during test; RESULTS ARE DUBIOUS.\n");
  36.   printf("\n");
  37.   exit(damaged != 1);
  38. }
  39.  
  40. /* ARGSUSED */
  41. void
  42. ensureEventsGotten(int value)
  43. {
  44.   /* Hack.  Creating a new window _ensures_ any outstanding
  45.      expose event from popping the window will be retrieved. */
  46.   glutCreateWindow("dummy");
  47.   glutHideWindow();
  48.   glutTimerFunc(1, report, 0);
  49. }
  50.  
  51. void
  52. displayDone(void)
  53. {
  54.   if (glutLayerGet(GLUT_NORMAL_DAMAGED))
  55.     damaged++;
  56. }
  57.  
  58. /* ARGSUSED */
  59. void
  60. done(int value)
  61. {
  62.   glFinish();
  63.   timeEnd = glutGet(GLUT_ELAPSED_TIME);
  64.   error = glGetError();
  65.  
  66.   /* Pop the window.  If the window was obscured by another
  67.      window during the test, raising the window should generate
  68.      an expose event we want to catch. */
  69.   glutPopWindow();
  70.  
  71.   /* The test is over so only notice an expose and do not run
  72.      the testRender routine. */
  73.   glutDisplayFunc(displayDone);
  74.   glutTimerFunc(1, ensureEventsGotten, 0);
  75. }
  76.  
  77. void
  78. display(void)
  79. {
  80.   int i;
  81.  
  82.   if (glutLayerGet(GLUT_NORMAL_DAMAGED)) {
  83.     damaged++;
  84.     if (damaged == 1) {
  85.       glutTimerFunc(testMinimumTestTime * 1000, done, 0);
  86.       timeStart = glutGet(GLUT_ELAPSED_TIME);
  87.     }
  88.   }
  89.   for (i = 0; i < testIterationsStep; i++) {
  90.     testRender();
  91.     renders++;
  92.   }
  93.   glutPostRedisplay();
  94. }
  95.  
  96. void
  97. visible(int state)
  98. {
  99.   if (state == GLUT_NOT_VISIBLE)
  100.     damaged++;
  101. }
  102.  
  103. int
  104. main(int argc, char **argv)
  105. {
  106.   char *newArgv[100];
  107.   int newArgc, i;
  108.  
  109.   /* Defaults; testInit may override these. */
  110.   testIterationsStep = 5;
  111.   testDisplayMode = GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH;
  112.   testMinimumTestTime = 10;  /* seconds */
  113.  
  114.   glutInit(&argc, argv);
  115.   newArgc = 1;
  116.   newArgv[0] = argv[0];
  117.   for (i = 1; i < argc; i++) {
  118.     if (!strcmp("-time", argv[i])) {
  119.       i++;
  120.       if (argv[i] == NULL) {
  121.         fprintf(stderr, "%s: -time option needs argument\n", argv[0]);
  122.         exit(1);
  123.       }
  124.       testMinimumTestTime = (int) strtol(argv[i], NULL, 0);
  125.     } else if (!strcmp("-mode", argv[i])) {
  126.       i++;
  127.       if (argv[i] == NULL) {
  128.         fprintf(stderr, "%s: -mode option needs argument\n", argv[0]);
  129.         exit(1);
  130.       }
  131.       testDisplayMode = (int) strtol(argv[i], NULL, 0);
  132.     } else if (!strcmp("-iters", argv[i])) {
  133.       i++;
  134.       if (argv[i] == NULL) {
  135.         fprintf(stderr, "%s: -mode option needs argument\n", argv[0]);
  136.         exit(1);
  137.       }
  138.       testIterationsStep = (int) strtol(argv[i], NULL, 0);
  139.     } else {
  140.       newArgv[newArgc] = argv[i];
  141.       newArgc++;
  142.     }
  143.   }
  144.   newArgv[newArgc] = NULL;
  145.  
  146.   glutInitDisplayMode(testDisplayMode);
  147.   glutCreateWindow("OpenGL performance test");
  148.   glutDisplayFunc(display);
  149.   glutVisibilityFunc(visible);
  150.   testInit(newArgc, newArgv,
  151.     glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
  152.   glutMainLoop();
  153.   return 0;             /* ANSI C requires main to return int. */
  154. }
  155.